//
//  SerarchResultVC.swift
//  甜言蜜语
//
//  Created by yingli_yanfa_liuxingyu on 16/8/12.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
import SVProgressHUD
class SerarchResultVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!
    //type == 1 || type == 3
    var newsModelList: [NewsModel]?{
        didSet{
            self.tableView.reloadData()
        }
    }
    // type == 2
    var starModelList: [starBusinessInfo]!{
        didSet {
            self.tableView.reloadData()
        }
        
    }
    
    var keyword: String = ""
    var type: String = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        publicMethod.setHUDStyle()
        self.tableView.delegate = self
        self.tableView.dataSource = self
        publicMethod.hiddleExtendCellForTableView(self.tableView)
        if self.type == "2"{
            self.getStarSearchResult(keyword, type: type)
            
        }else{
            self.getSearchResult(keyword, type: type)
        }
        
    }
    override func viewWillDisappear(animated: Bool) {
        SVProgressHUD.dismiss()
        super.viewWillDisappear(animated)
    }
    //MARK:tableviewDataSource
    
   func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if(cell.respondsToSelector(Selector("setSeparatorInset:"))) {
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        if(cell.respondsToSelector(Selector("setLayoutMargins:"))) {
            cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
        }
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.type == "2"{
            if self.starModelList != nil{
                return self.starModelList.count
            }else{
                return 0
            }
        }else{
            if self.newsModelList != nil{
                return (self.newsModelList?.count)!
            }else{
                return 0
            }
        }
        
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if self.type == "2"{
            return 115.0
        }else{
            let newsModel = self.newsModelList![indexPath.row]
            return newsModel.cellType!.cellHeight()
        }
        
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if self.type == "2"{
            let cell = tableView.dequeueReusableCellWithIdentifier("specialChannelCell", forIndexPath: indexPath) as! specialChannelCell
            let model = starModelList[indexPath.row]
            
            cell.iv.sd_setImageWithURL(NSURL(string: model.timg), placeholderImage: UIImage(named: "placeholder"))
            cell.addressLbl.text = String(format: "%@%@%@",model.unitaddress ,model.unitarea ,model.unittype)
            cell.id = model.id
            cell.intruductLbl.text = model.introduction
            cell.titleLbl.text = model.unitname
            return cell
            
        }else{
            let newsModel = self.newsModelList![indexPath.row]
            
            let cell = tableView.dequeueReusableCellWithIdentifier(newsModel.cellType.rawValue, forIndexPath: indexPath) as! NewsCell
            cell.newsModel = newsModel
            
            return cell
        }
    }
    
    
    // MARK: - Table view delegate
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
        if self.type == "2"{
            let sb = UIStoryboard(name: "NewsList", bundle: nil)
            let vc = sb.instantiateViewControllerWithIdentifier("SpecialStarDetailVC") as! SpecialStarDetailVC
            vc.id = self.starModelList[indexPath.row].id
            self.navigationController?.pushViewController(vc, animated: true)

        }else{
            if self.newsModelList![indexPath.row].type == 4{
                //特殊新闻
                let sb = UIStoryboard(name: "NewsList", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("SpecialNewsController") as! SpecialNewsController
                vc.newsModel = self.newsModelList![indexPath.row]
                self.navigationController?.pushViewController(vc, animated: true)
                
            }else if self.newsModelList![indexPath.row].type == 5{
                //视频新闻
                let sb = UIStoryboard(name: "NewsList", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("DetaillNewsController") as! DetaillNewsController
                vc.newsModel = self.newsModelList![indexPath.row]
                self.navigationController?.pushViewController(vc, animated: true)
                
            }else if self.newsModelList![indexPath.row].type == 6{
                //直播新闻
                let sb = UIStoryboard(name: "NewsList", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("DetaillNewsController") as! DetaillNewsController
                vc.newsModel = self.newsModelList![indexPath.row]
                self.navigationController?.pushViewController(vc, animated: true)
            }else if self.newsModelList![indexPath.row].type == 8{
                //淘宝新闻
                let sb = UIStoryboard(name: "NewsList", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("DetaillNewsController") as! DetaillNewsController
                vc.newsModel = self.newsModelList![indexPath.row]
                self.navigationController?.pushViewController(vc, animated: true)
            }else{
                //正常新闻
                let sb = UIStoryboard(name: "NewsList", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("DetaillNewsController") as! DetaillNewsController
                vc.newsModel = self.newsModelList![indexPath.row]
                self.navigationController?.pushViewController(vc, animated: true)
            }
        }
        
    }

    //MARK:NETWORKING
    func getSearchResult(keyword: String, type: String){
        DataTool.keywordSearch(keyword, type: type, completionHandler:{ (newModel) in
            guard let newDataes = newModel else {
                return
            }
            print(newDataes)
            self.newsModelList = newDataes
            if self.newsModelList?.count == 0{
                
                SVProgressHUD.showErrorWithStatus("没有找到")
            }
        })
    }
    
    func getStarSearchResult(keyword: String, type: String){
        DataTool.keywordStarSearch(keyword, type: type, completionHandler:{ (newModel) in
            guard let newDataes = newModel else {
                return
            }
            print(newDataes)
            self.starModelList = newDataes
            
            if self.starModelList.count == 0{
               
                SVProgressHUD.showErrorWithStatus("没有找到")
            }
        })

    }
}
